package com.macro.mall.controller;

import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dto.PmsBrandParam;
import com.macro.mall.model.PmsBrand;
import com.macro.mall.model.PmsBrandExample;
import com.macro.mall.service.PmsBrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "PmsBrandController" ,description = "商品品牌管理")
@RestController
@RequestMapping("/brand")
public class PmsBrandController {

    @Autowired
    private PmsBrandService pmsBrandService;

    @ApiOperation("根据编号查询品牌信息")
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public CommonResult<PmsBrand> selectById(@PathVariable("id") Long id){
        PmsBrand pmsBrand = pmsBrandService.selectByPrimaryKey(id);
        return CommonResult.success(pmsBrand);
    }

    @ApiOperation("添加品牌")
    @RequestMapping(value = "/create" , method = RequestMethod.POST)
    public CommonResult insert(@RequestBody PmsBrandParam brandParam){
        int a = pmsBrandService.insertSelective(brandParam);
        CommonResult commonResult;
        if (a == 1){
            commonResult = CommonResult.success(brandParam);
        }else {
            commonResult = CommonResult.failed("添加失败");
        }
        return commonResult;
    }

    @ApiOperation("删除品牌")
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    public CommonResult deleteOne(@PathVariable("id") Long id){
        int a = pmsBrandService.deleteByPrimaryKey(id);
        CommonResult commonResult;
        if (a == 1){
            commonResult = CommonResult.success(id);
        }else {
            commonResult = CommonResult.failed("删除失败");
        }
        return commonResult;
    }

    @ApiOperation("批量删除品牌")
    @RequestMapping(value = "/delete/batch",method = RequestMethod.POST)
    public CommonResult deleteAll(@RequestBody List<Long> ids){
        int a = pmsBrandService.deleteAll(ids);
        CommonResult commonResult;
        if (a == 1){
            commonResult = CommonResult.success(ids);
        }else {
            commonResult = CommonResult.failed("删除失败");
        }
        return commonResult;
    }

    @ApiOperation("根据品牌名称分页获取品牌列表")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public CommonResult<CommonPage<PmsBrand>> getPageList(@RequestParam(value = "pageNum" ,defaultValue = "1") Integer pageNum,
                                                          @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        List<PmsBrand> list = pmsBrandService.selectByExample(pageSize,pageNum);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation("获取全部品牌列表")
    @RequestMapping(value = "/listAll",method = RequestMethod.GET)
     public CommonResult<List<PmsBrand>> getAll(){
         List<PmsBrand> list = pmsBrandService.selectByExampleWithBLOBs(new PmsBrandExample());
         return CommonResult.success(list);
     }

    @ApiOperation("更新品牌")
    @RequestMapping(value = "/update/{id}",method = RequestMethod.POST)
     public CommonResult update(@PathVariable("id") Long id , @RequestBody PmsBrandParam brandParam){
        brandParam.setId(id);
        int a = pmsBrandService.updateByPrimaryKeySelective(brandParam);
         CommonResult commonResult;
         if (a == 1){
             commonResult = CommonResult.success(brandParam);
         }else {
             commonResult = CommonResult.failed("更新失败");
         }
         return commonResult;
     }

    @ApiOperation("批量更新厂家制造商状态")
    @RequestMapping(value = "/update/factoryStatus",method = RequestMethod.POST)
     public CommonResult updateFactory(@RequestBody List<Long> ids){
        int a = 0;
        for (int i= 0 ; i<ids.size();i++){
            PmsBrandParam pms = new PmsBrandParam();
            pms.setId(ids.get(i));
            pms.setFactoryStatus(pmsBrandService.selectByPrimaryKey(ids.get(i)).getFactoryStatus());
            a = pmsBrandService.updateByExampleSelective(pms);
        }
         CommonResult commonResult;
         if (a == 1){
             commonResult = CommonResult.success(ids);
         }else {
             commonResult = CommonResult.failed("更新失败");
         }
         return commonResult;
     }

    @ApiOperation("批量更新显示状态")
    @RequestMapping(value = "/update/showStatus",method = RequestMethod.POST)
    public CommonResult updateShow(@RequestBody List<Long> ids){
        int a = 0;
        for (int i= 0 ; i<ids.size();i++){
            PmsBrandParam pmsBrand = new PmsBrandParam();
            pmsBrand.setId(ids.get(i));
            pmsBrand.setShowStatus(pmsBrandService.selectByPrimaryKey(ids.get(i)).getShowStatus());
            a = pmsBrandService.updateShow(pmsBrand);
        }
        CommonResult commonResult;
        if (a == 1){
            commonResult = CommonResult.success(ids);
        }else {
            commonResult = CommonResult.failed("更新失败");
        }
        return commonResult;
    }
}
